﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>新闻</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <style type="text/css">
        <!--
        .title {
            font-size: 16px;
            text-align: center;
            clear: both;
            float: left;
            padding-top: 20px;
            padding-bottom: 20px;
            width: 100%;
            background-color: #6699CC;
        }

        .content {
            width: 700px;
            text-align: left;
            margin-top: 10px;
            margin-right: auto;
            margin-bottom: 10px;
            margin-left: auto;
        }

    </style>
</head>

<body>
<div>
    <div class="title">新闻抓取</div>
    <div style="clear:both">
        <hr/>
    </div>
    <div class="content">
        <?php

        /***************************
        @name zf
        @time 2014-02-19
        @parameter url：网址
        @func  获取新闻标题、时间、新闻url
         ***************************/
        function Getnews($url)
        {
            //$url = "http://tech.ifeng.com/roll/rtlist_20140218/1.shtml";//网址
            //print_r(Get_news($url,$count));
            $handle = fopen($url, "r");
            $newsarr = array();
            //排除非新闻类的标签
            while (!feof($handle)) {
                $line = fgets($handle);
                $count = 0;
                str_replace("<h4>", "", $line, $count); //匹配新闻中的标签，进行排除
                if ($count > 0) {
                    $newarr[] = $line;
                }
            }
            fclose($handle);
            $c = 0;
            $res = array(); //用正则匹配函数
            foreach ($newarr as $k => $v) {
                $ptr = "/<li><h4>(.*)<\/h4><a href=\"(.*)\" target=\"_blank\">(.*)<\/a>/"; //抓取正则表达式
                preg_match_all($ptr, $v, $str, PREG_SET_ORDER); //抓取匹配新闻
                //对新闻进行重新赋值数组
                if (isset($str)) {
                    $res[$c] = array();
                    $res[$c][] = $str[0][1];
                    $res[$c][] = $str[0][2];
                    $res[$c][] = $str[0][3];
                    $c = $c + 1;
                }
            }
            return $res;

        }

        /***************************
        @name zf
        @time 2014-02-19
        @parameter page:显示页数 totalnum:数据总量 numPerPage:每页显示数量
        @func  翻页功能
         ***************************/
        function  pagefunc($page, $totalnum, $numPerPage)
        {
            //得到分页导航
            $pagestr = '';
            //如果不是第一页，则有首页导航
            if ($page > 1) {
                $pagestr .= '<a href="require1.php?page=1 id="firstpage">首  页</a>&nbsp;&nbsp;';
            } else {
                $pagestr .= '首  页&nbsp;&nbsp;';
            }
            if ($page == 1) {
                if ($totalnum > $numPerPage) {
                    $pagestr .= '<a href="require1.php?page=' . ($page + 1) . '">下一页</a>&nbsp;&nbsp;';
                }
            } else if ($page == ceil($totalnum / $numPerPage)) {
                $pagestr .= '<a href="require1.php?page=' . ($page - 1) . '">上一页</a>&nbsp;&nbsp;';
            } else {
                $pagestr .= '<a href="require1.php?page=' . ($page + 1) . '">下一页</a>&nbsp;&nbsp;<a href="require1.php?page=' . ($page - 1) . '">上一页</a>&nbsp;&nbsp;';
            }
            //如果不是末页，则有末页导航
            if ($page < ceil($totalnum / $numPerPage)) {
                $pagestr .= '<a href="require1.php?page=' . ceil($totalnum / $numPerPage) . '">末  页</a>';
            } else {
                $pagestr .= '末  页';
            }
            $pagestr .= '&nbsp;&nbsp;共&nbsp;' . $totalnum . '&nbsp;条记录';
            return $pagestr;
        }

        /***************************
         * 例子:抓取新闻
         ***************************/
        $url = "http://tech.ifeng.com/roll/rtlist_20140218/1.shtml"; //网址
        //抓取新闻
        $res = Getnews($url);
        //得到分页导航
        //$pagestr='';
        $numPerPage = 10; //每页显示数据条数
        $totalnum = count($res); //数据总条数
        $page = isset($_GET['page']) ? $_GET['page'] : 1; //得到显示第几页
        $offset = ($page - 1) * $numPerPage;
        //获取翻页功能
        $pagestr = pagefunc($page, $totalnum, $numPerPage);

        echo '
		<table cellpadding="0" border="0" cellspacing="1" class="base_line" width="800">
		<tr style="font-weight:bold;text-align:center;">
			<td width="60">时间</td>
			<td width="200">新闻信息</td>
			<td width="80">操作</td>
		</tr>';
        //逐条显示新闻信息
        for ($i = $offset; $i < $numPerPage * $page; $i++) {
            //print_r($res[$i]);
            echo '<tr><td style="text-align:center">' . $res[$i][0] . '</td>';
            echo '<td>' . $res[$i][2] . '</td>';
            echo '<td><a href="' . $res[$i][1] . '" target="_blank">查看</a>  <a href="#">删除</a>  <a href="#">编辑</a></td></tr>';
        }
        echo '<tr><td colspan="100%">' . $pagestr . '<td><tr>';
        echo '</table>';
        $con = mysql_connect('localhost', 'root', ''); //连接主机,root为你的服务器名
        if ($con) {
            echo '主机连好';
        } else {
            echo '连接失败';
        }
        $sql = 'create database if not exists data character set utf8;'; //创建数据库db
        if (mysql_query($sql, $con)) //判断
        {
            echo '数据库创建好了';
        } else {
            echo '数据库创建失败';
        }

        mysql_select_db('data', $con);
        mysql_query('set names utf8');
        $sql = "create table if not exists m_table(
					id integer not null auto_increment primary key comment '序号',
					time char(100) not null comment '时间',
					news varchar(200) not null comment '新闻',
					content varchar(100) not null comment '网址'
					)
					COLLATE='utf8_general_ci';";
        if (mysql_query($sql, $con)) {
            echo 'table create success;';
        } //判断
        else {
            die('table create failed' . mysql_error());
        }


        for ($j = 0; $j < $totalnum; $j++) {
            $t = $res[$j][0];
            $n = $res[$j][2];
            $c = $res[$j][1];
            $val = "insert into m_table(time,news,content)values('$t', '$n', '$c');";
            var_dump($val);
            if (!mysql_query($val, $con)) {
                die('数据传输失败' . mysql_error());
            }
        }
        mysql_close($con);
        ?>
    </div>
    <div style="clear:both">
        <hr/>
    </div>
</div>
</body>
</html>
